<?php

namespace App\Services;

use App\Models\SmsLog;
use Carbon\Carbon;
use Illuminate\Http\Request;

class Captcha
{
    /**
     * 验证短信验证码
     * @param Request $request
     * @return bool
     */
    public static function verificationSmsCaptcha(Request $request): bool
    {
        try {
            $smsLog = SmsLog::query()
                ->where('phone', $request->get('phone', ''))
                ->orderBy('created_at', 'desc')
                ->firstOrFail()
                ->toArray();
        } catch (\Exception $e) {
            return false;
        }

        return (int) $smsLog['properties']['code'] === (int) $request->get('sms_captcha')
            && $smsLog['properties']['uuid'] === $request->get('uuid')
            && Carbon::now()->isBefore(Carbon::parse($smsLog['properties']['expired_at']));
    }
}
